﻿#include "widget.h"

#include <QDebug>
#include <QMetaType>
#include <QTimer>
#include <QVector>

#include "./ui_widget.h"
#include <chrono>

#include "libdaq/device/DAQ122/daq122.h"
using namespace libdaq::device;

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
  ui->setupUi(this);
  // 注册
  qRegisterMetaType<QVector<QVector<double>>>("QVector<QVector<double>>");

  for (int var = 0; var < 8; ++var) {
    ui->widget->addGraph();
  }

  ui->widget->xAxis->setRange(0, 1000);
  ui->widget->yAxis->setRange(-5, 5);
  ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes);

  timer_ = new QTimer();
  connect(timer_, &QTimer::timeout, this, &Widget::ProcessData);
}

Widget::~Widget() { delete ui; }

void Widget::on_pushButton_clicked() {
  // 初始化DAQ122
  if (!daq122.InitializeDevice()) {
    qDebug() << u8"初始化DAQ122失败";
  }

  // 连接DAQ122
  if (!daq122.ConnectedDevice()) {
    qDebug() << u8"连接设备失败";
  }

  // 配置采样率为200k，电压为5V
  auto voltage_range = DAQVoltage::Voltage5V;
  auto sample_rate = DAQADCSampleRate::SampleRate200K;
  if (!daq122.ConfigureADCParameters(sample_rate, voltage_range)) {
    qDebug() << u8"配置DAQ122采样率失败";
  }

  // 配置为采集前4个通道的数据
  adc_channel_state_ = DAQADCChannel::AIN1 | DAQADCChannel::AIN2 | DAQADCChannel::AIN3 | DAQADCChannel::AIN4;
  daq122.ConfigADCChannel(adc_channel_state_);
}

void Widget::on_pushButton_2_clicked() {
  timer_->setInterval(500);
  timer_->start();
  if (daq122.StartADCCollection()) {
    qDebug() << u8"DAQ122开始采集数据";
  }
}

void Widget::on_pushButton_3_clicked() {
  // 停止采集
  if (daq122.StopADCCollection()) {
    qDebug() << u8"DAQ122停止采集数据";
  }
  timer_->stop();
}

void Widget::ProcessData() {
  auto legal_channel_size = daq122.GetADCLegalChannelSize();
  auto storage_depth = daq122.GetCurrentSampleRate();
  QVector<QVector<double>> receive_data(legal_channel_size);
  for (int var = 0; var < receive_data.size(); ++var) {
    receive_data[var].resize(storage_depth);
  }

  // 判断当前数据是否已经满足读取的条件
  if(!daq122.ADCDataIsReady(storage_depth)){
    return;
  }

  auto read_result = true;
  for (int i = 0; i < legal_channel_size; i++) {
    // 判断当前通道是否打开，没有打开则跳过
      if((adc_channel_state_ & (0b00000001) << i) == 0){
        continue;
    }

    // 读取数据并计算耗时
    auto start_time = std::chrono::high_resolution_clock::now();
    read_result = daq122.TryReadADCData(i, receive_data[i].data(), receive_data[i].size(), 1000);
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
    if(!read_result){
        qDebug() << "Error";
    } else {
        qDebug() << "Channel" << i << "read time:" << duration.count() << "us";
    }
  }

  QVector<double> x_data(storage_depth);
  for (int var = 0; var < storage_depth; ++var) {
    x_data[var] = 1000.0 / storage_depth * var;
  }

  if (read_result) {
    for (int var = 0; var < legal_channel_size; ++var) {
      ui->widget->graph(var)->setData(x_data, receive_data[var]);
    }
    ui->widget->replot(QCustomPlot::rpQueuedReplot);
  }
}
